<?php 
namespace XFCore\traits\api;
use lib\{Tips,PayTemplate};
/*
*支付模板处理
*/
trait SumitTrait{
    public function gopay($get){
        $trade_no = $get['trade_no'];
        if(empty($trade_no)){
            Tips::error("订单不存在！");
        }
        $srow = $this->db->orderrow($trade_no);
        if(!$srow){
            Tips::error("订单不存在！");
        }
        if($srow['price'] < 0.01){
            Tips::error("金额不合法！");
        }
        $userrow = $this->db->userrow($srow['pid'] , 'pid',"`INPUTFREE`,`pid`,`pay_template`");
        if(!$userrow){
            Tips::error("商户不存在！");
        }
        $QR_row  = $this->db->qrlistrow($srow['qr_id'],'id',"`id`,`cookie`,`alipayUId`,`type`");
        if(!$QR_row){
            Tips::error("二维码数据不存在！");
        }
        
        if($QR_row['type'] == 'alipay'){
            
            if($QR_row['cookie'] == 'xfqymckxy'){
                
                $this->Deal_Mckqr($srow , $userrow , $QR_row);
            }
        }
        $this->Deal_Pay($srow , $userrow , $QR_row);
        
    }
    protected function Deal_Mckqr($srow , $userrow , $QR_row){
        $userId = $QR_row['alipayUId']?:'';
        if(empty($userId)){
            Tips::error("未知错误！");
        }
        $Xf_MOSHI = $userrow['INPUTFREE']?:3;
        $alipayh5url_4   = $this->CurlUrl->ALIH5URL($Xf_MOSHI,$srow['price'],$userId,$srow['trade_no']);
        
        $mp3             = xfmp3($srow['price']);
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'QQ/')||strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')!==false){
            $startApp = '<a id="startApp2"></a>';
        }else{
            $startApp = '<a id="startApp1"></a>';
        }
        $type = $srow['type'];
        $t = true;
        
        include submittem.'qymck.php';
        exit;
    }
    protected function Deal_Pay($srow , $userrow , $QR_row){
        
        $mp3 = xfmp3($srow['price']);
        $mod = 'XUFEI_Pay';
        $typeName = strip_tags(pay_type($srow['type']));
        $loadfile = PayTemplate::load($mod,$userrow);
        
        $t = true;
        include $loadfile;
    }
    public function Mcode_get($get){
        $trade_no = $get['trade_no'];
        if(empty($trade_no)){
            Tips::error("订单不存在！");
        }
        $srow = $this->db->orderrow($trade_no);
        if(!$srow){
            exitjson(-1,'订单不存在!');
        }
        if($srow['price'] < 0.01){
            exitjson(-1,'金额不合法!');
        }
        $userrow = $this->db->userrow($srow['pid'] , 'pid',"`INPUTFREE`,`pid`");
        if(!$userrow){
            exitjson(-1,'商户不存在!');
        }
        $QR_row  = $this->db->qrlistrow($srow['qr_id'],'id');
        if(!$QR_row){
            exitjson(-1,'二维码数据不存在!');
        }
        if($srow['status']==1){
            $url  = creat_callback($srow);
            $data = array("backurl"=>$url['return']);
            exitjson(200,'订单支付成功',['data'=>$data]);
        }
        if($srow['outtime']<=time()){
            exitjson(0,'支付超时！');
        }
        $data =  $this->datatypeqrcode($srow,$QR_row,$userrow);
        
        exitjson(100,'success',['data'=>$data()]);
    }
    protected function datatypeqrcode($srow,$QR_row,$userrow){
        $trade_no = $srow['trade_no'];
        $xf_moshi = $userrow['INPUTFREE']?:3;
        $defaultData = [
            "qrcode" => $this->CurlUrl->b64_qrcode($QR_row['qr_url']),
            "money" => $srow['price'] ?? null,
            'url' => urlencode($QR_row['qr_url'])
        ];
        $handlers = [
            'alipay' => function() use ($QR_row, $srow, $trade_no, $xf_moshi) {
                //xf_type = 1 pc  xf_type = 3 APP
                return $this->handleAlipayPayment($QR_row, $srow, $trade_no, $xf_moshi);
            },
            'wxpay' => function() use ($QR_row, $srow, $trade_no) {
                return $this->handleWxPayment($QR_row, $srow, $trade_no);
            },
            'qqpay' => function() use ($QR_row, $srow, $trade_no) {
                return $this->handleQQPayment($QR_row, $srow, $trade_no);
            },
            'usdt' => function() use ($QR_row) {
                check_usdt($QR_row,$srow);
                return [
                    "qrcode" => $this->CurlUrl->b64_qrcode(urlencode($QR_row['qr_url'])),
                    "money" => $srow['price'] ?? null,
                    'url' => urlencode($QR_row['qr_url'])
                ];
            }
        ];
        return $handlers[$srow['type']] ?? function() use ($defaultData) {
            return $defaultData;
        };
    }
    
    protected function handleAlipayPayment($QR_row, $srow, $trade_no, $xf_moshi) {
        if ($QR_row['hook_type'] == 0) {
            if ($QR_row['cookie'] != 'xfqydmfxy' && $QR_row['cookie'] != 'xfqymckxy') {
                return $this->handleRegularAlipay($QR_row, $srow, $trade_no, $xf_moshi);
            } elseif ($QR_row['cookie'] == 'xfqymckxy') {
                return $this->handleMckAlipay($QR_row, $srow, $trade_no, $xf_moshi);
            }
        } elseif ($QR_row['hook_type'] == 1) {
            if($QR_row['xf_type'] == 1){
                return $this->handleRegularAlipay($QR_row, $srow, $trade_no, $xf_moshi);
            }else if($QR_row['xf_type'] == 3){
                return $this->qrcodeHookType1($QR_row,$srow);
            }
            
        }
    }
    protected function handleRegularAlipay($QR_row, $srow, $trade_no, $xf_moshi) {
        if($QR_row['hook_type'] == 0)ALIQQ_money_notify($QR_row,$srow);
        $userId = getSubstr(base64_decode($QR_row['cookie']), "CLUB_ALIPAY_COM=", ";");
        $qr_url = $this->CurlUrl->aliqrurl($userId, $trade_no, $srow['price'], $xf_moshi);
        
        return [
            "qrcode" => $this->CurlUrl->b64_qrcode($qr_url),
            "money" => $srow['price'],
            'url' => urlencode($qr_url),
            'ailh5url'=>$this->CurlUrl->ALIH5URL($xf_moshi,$srow['price'],$userId,$srow['trade_no'])
        ];
    }
    protected function handleMckAlipay($QR_row, $srow, $trade_no, $xf_moshi) {
        $userId = $QR_row['alipayUId'];
        xufeiqymoneymck($QR_row, $srow);
        $qr_url = $this->CurlUrl->aliqrurl($userId, $trade_no, $srow['price'], $xf_moshi);
        return [
            "qrcode" => $this->CurlUrl->b64_qrcode($qr_url),
            "money" => $srow['price'],
            'url' => urlencode($qr_url)
        ];
    }
    
    protected function handleQQPayment($QR_row, $srow, $trade_no){
        if($QR_row['hook_type'] == 0)ALIQQ_money_notify($QR_row,$srow);
        $qr_url = $this->CurlUrl->qqmsurl($srow['price'], $trade_no, $QR_row['cookie']);
        return [
            "qrcode" => $this->CurlUrl->b64_qrcode(urlencode($qr_url)),
            "money" => $srow['price'],
            'url' => urlencode($qr_url)
        ];
    }
    
    protected function handleWxPayment($QR_row, $srow, $trade_no){
        if ($QR_row['hook_type'] == 0 || $QR_row['hook_type'] == 1) {//PC 和店员
            return $this->handleWxStaffOrPc($QR_row, $srow);
        }elseif ($QR_row['hook_type'] == 2){
            return $this->handleWxHookType2($QR_row, $srow);
        }else if($QR_row['hook_type'] == 1){
            return $this->qrcodeHookType1($QR_row,$srow);
        }
        
        
    }
    //pc和店员处理 
    protected function handleWxStaffOrPc($QR_row, $srow){
        if ($QR_row['xf_type'] == '5' || $QR_row['xf_type'] == '4') {//赞赏码和 收款单
            return [
                    "qrcode" => $QR_row['qr_url'],
                    "money" => $srow['price'],
                    'url' => urlencode($QR_row['qr_url'])
                ];
        }else {
            return [
                "qrcode" =>$this->CurlUrl->b64_qrcode(urlencode($QR_row['qr_url'])),
                "money" => $srow['price'],
                'url' => urlencode($QR_row['url'])
            ];
        }
    }
    protected function handleWxHookType2($QR_row, $srow){
        
        $qr_url = WxYun_Notidfy($QR_row, $srow);
        $qr_url = empty($srow['pay_url']) ? $qr_url:$srow['pay_url'];
        
        if ($QR_row['xf_type'] == '8') {//赞赏码
            return [
                    "qrcode" => $qr_url,
                    "money" => $srow['price'],
                    'url' => urlencode($qr_url)
                ];
        }else {
            return [
                "qrcode" =>$this->CurlUrl->b64_qrcode(urlencode($qr_url)),
                "money" => $srow['price'],
                'url' => urlencode($qr_url)
            ];
        }
    }
    protected function qrcodeHookType1($QR_row , $srow){
        return [
            "qrcode" => $this->CurlUrl->b64_qrcode(urlencode($QR_row['qr_url'])),
            "money" => $srow['price'],
            'url' => urlencode($QR_row['qr_url'])
        ];
    }
    
    
    
    
}
?>